package com.zsy.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import sdfi.lee.Properties.ReaderProperties;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
/**
 * 带连接池的数据库工具类 
 * @author zhousuying
 * @email  zhousy@sdcncsi.com.cn
 */
public final class DBUtil {
	private static DBUtil instance;

	public ComboPooledDataSource ds;
	private static String c3p0Properties = "/c3p0.properties";

	private DBUtil() throws Exception {
		Properties p = new Properties();
		p.load(this.getClass().getResourceAsStream(c3p0Properties));
		ds = new ComboPooledDataSource();
		ds.setDriverClass(p.getProperty("driverClass"));
		ds.setJdbcUrl(p.getProperty("jdbcUrl"));  
		ds.setUser(p.getProperty("user"));  
		ds.setPassword(p.getProperty("password")); 
		ds.setMaxPoolSize(Integer.parseInt(p.getProperty("maxPoolSize")));
		ds.setInitialPoolSize(Integer.parseInt(p.getProperty("initialPoolSize")));
		System.out.println(p.getProperty("jdbcUrl"));
	}

	public static final DBUtil getInstance() {
		if (instance == null) {
			try {
				instance = new DBUtil();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return instance;
	}
/*
	public synchronized final Connection getConnection() {
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	*/
	
	public static Connection getConnection() throws SQLException {
		Connection conn = null;
		String mysql_search_ip="";
		String mysql_search_db="";
		String mysql_search_user="";
		String mysql_search_pass="";
		ReaderProperties reader=new ReaderProperties();
		try
		{
			
			mysql_search_ip=reader.getProperty("mysql_search_ip");
			mysql_search_db=reader.getProperty("mysql_search_db");
			mysql_search_user=reader.getProperty("mysql_search_user");
			mysql_search_pass=reader.getProperty("mysql_search_pass");
		}
		catch(Exception ee)
		{
			 mysql_search_ip="localhost";
			 mysql_search_db="crawl";
			 mysql_search_user="root";
			 mysql_search_pass="root";
			ee.printStackTrace();
		}
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://"+mysql_search_ip+":3306/"+mysql_search_db;
			
			conn = DriverManager.getConnection(url,mysql_search_user,mysql_search_pass);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} 
			return conn;
	}

	
	

	protected void finalize() throws Throwable {
		DataSources.destroy(ds); // 
		super.finalize();
	}

	public static void main(String[] args) throws SQLException{
		long begin = System.currentTimeMillis();
		for(int i=0;i<100;i++){
			Connection conn = DBUtil.getInstance().getConnection();
			ResultSet rs = conn.createStatement().executeQuery("select * from crawlurl");
			while(rs.next()){
				
			}
			conn.close();
		}
		
		long end = System.currentTimeMillis();
		System.out.println(end-begin);
	}
	
}
